package com.users.util;

import java.util.ArrayList;
import java.util.List;

public class PaginationSupport{

	public static int PAGESIZE = 10; // 默认页面显示纪录的数目

	private int pageSize = PAGESIZE; // 页面显示的纪录数目

	private List items; // 返回结果集

	private int totalCount; // 总记录数

	private int[] indexes = new int[0]; // 保存当前索引的纪录数

	private int startIndex = 0; // 每页第一条数据的索引

	private int laststartIndex; // 最后一页第一条数据的索引

	private int pages; // 总页数

	private int currentPage; // 当前页码

	public PaginationSupport() {}
	/**
	 * 2参 构造函数
	 * @param items
	 * @param totalCount
	 */
	public PaginationSupport(List items, int totalCount) {
		setPageSize(PAGESIZE);
		setTotalCount(totalCount);
		setItems(items);
		setStartIndex(0);
	}

	/**
	 * 3参 构造函数
	 * @param items
	 * @param totalCount
	 * @param startIndex
	 */
	public PaginationSupport(List items, int totalCount, int startIndex) {
		setPageSize(PAGESIZE);
		setTotalCount(totalCount);
		setItems(items);
		setStartIndex(startIndex);
	}

	/**
	 * 4参 构造函数
	 * @param items
	 * @param totalCount
	 * @param pageSize
	 * @param startIndex
	 */
	public PaginationSupport(List items, int totalCount, int pageSize, int startIndex) {
		setPageSize(pageSize);
		setTotalCount(totalCount);
		setItems(items);
		setStartIndex(startIndex);
	}

	/**
	 * 返回结果集
	 * @return
	 */
	public List getItems() {
		return items;
	}

	/**
	 * 设置结果集
	 * @return
	 */
	public void setItems(List items) {
		this.items = items;
	}
	/**
	 * 返回页面显示的纪录数目
	 * @return
	 */
	public int getPageSize() {
		return pageSize;
	}
	/**
	 * 设置页面显示的纪录数目
	 * @param pageSize
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	/**
	 * 返回总记录数
	 * @return
	 */
	public int getTotalCount() {
		return totalCount;
	}
	/**
	 * 设置总记录数
	 * @param totalCount
	 */
	public void setTotalCount(int totalCount) {
		if (totalCount > 0) {
			this.totalCount = totalCount;
			int count = totalCount / pageSize;
			if (totalCount % pageSize > 0) count++;
			indexes = new int[count];
			for (int i = 0; i < count; i++) {
				indexes[i] = pageSize * i;
			}
		} else {
			this.totalCount = 0;
		}
	}

	public int[] getIndexes() {
		return indexes;
	}

	public void setIndexes(int[] indexes) {
		this.indexes = indexes;
	}
	/**
	 * 返回每页第一条数据的索引
	 * @return
	 */
	public int getStartIndex() {
		return startIndex;
	}
	/**
	 * 设置每页第一条数据的索引
	 * @param startIndex
	 */
	public void setStartIndex(int startIndex) {
		if (totalCount <= 0) this.startIndex = 0;
		else if (startIndex >= totalCount) this.startIndex = indexes[indexes.length - 1];
		else if (startIndex < 0) this.startIndex = 0;
		else {
			this.startIndex = indexes[startIndex / pageSize];
		}
	}
	/**
	 * 返回前一页第一条记录的索引
	 * @return
	 */
	public int getNextIndex() {
		int nextIndex = getStartIndex() + pageSize;
		if (nextIndex >= totalCount) return getStartIndex();
		else return nextIndex;
	}
	/**
	 * 返回后一页第一条记录的索引
	 * @return
	 */
	public int getPreviousIndex() {
		int previousIndex = getStartIndex() - pageSize;
		if (previousIndex < 0) return 0;
		else return previousIndex;
	}

	/**
	 * 设置最后一页第一条数据的索引
	 * @param laststartIndex
	 */
	public void setLaststartIndex(int laststartIndex) {
		this.laststartIndex = laststartIndex;
	}
	/**
	 * 返回最后一页第一条数据的索引
	 * @param laststartIndex
	 */
	public int getLaststartIndex() {
		int[] indexes = this.getIndexes();
		if (indexes != null && indexes.length > 0) {
			laststartIndex = indexes[indexes.length - 1];
		}
		return laststartIndex;

	}

	/**
	 * 设置总页数
	 * @param pages
	 */
	public void setPages(int pages) {
		this.pages = pages;
	}

	/**
	 * 返回总页数
	 * @return
	 */
	public int getPages() {
		if (getTotalCount() % pageSize == 0) { return getTotalCount() / pageSize; }
		return (getTotalCount() / pageSize) + 1;
	}
	/**
	 * 设置当前页码
	 * @param currentPage
	 */
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	/**
	 * 返回当前页码
	 * @return
	 */
	public int getCurrentPage() {
		return (getStartIndex() / pageSize) + 1;
	}

	/**
	 * 记录显示的页码组
	 * @param totalPages
	 * @param currentPage
	 * @return
	 */
	public ArrayList<Integer> page(int totalPages, int currentPage) {
		int adjacents = 3;// 这个参数可以调ArrayList的长度，试试就知道了
		ArrayList<Integer> result = new ArrayList<Integer>();
		if (totalPages < (5 + (adjacents * 2))) { // not enough links to make it
			// worth breaking up
			writeNumberedLinks(1, totalPages, result);
		} else {
			if ((totalPages - (adjacents * 2) > currentPage) && (currentPage > (adjacents * 2))) { // in the middle
				writeNumberedLinks(1, 1, result);
				writeElipsis(result);
				writeNumberedLinks(currentPage - adjacents - 1, currentPage + adjacents, result);
				writeElipsis(result);
				writeNumberedLinks(totalPages, totalPages, result);
			} else if (currentPage < (totalPages / 2)) {
				writeNumberedLinks(1, 3 + (adjacents * 2), result);
				writeElipsis(result);
				writeNumberedLinks(totalPages, totalPages, result);
			} else { // at the end
				writeNumberedLinks(1, 1, result);
				writeElipsis(result);
				writeNumberedLinks(totalPages - (2 + (adjacents * 2)), totalPages, result);
			}
		}
		return result;
	}

	/**
	 * @param result
	 * 
	 */
	private void writeElipsis(ArrayList<Integer> result) {
		result.add(-1);// -1是用来打点的
	}

	/**
	 * @param i
	 * @param lastIndex
	 * @param result
	 */
	private void writeNumberedLinks(int i, int lastIndex, ArrayList<Integer> result) {
		for (int d = i; d <= lastIndex; d++) {
			result.add(d);
		}
	}

}
